<template>
  <div>
    <el-form ref="queryForm" :model="queryParams" size="mini" inline style="white-space: nowrap; overflow-x: auto">
      <el-form-item prop="countYear">
        <el-date-picker
          v-model="queryParams.countYear"
          type="year"
          value-format="yyyy"
          placeholder="选择年"
        />
      </el-form-item>
      <el-form-item prop="countMonth">
        <el-date-picker
          v-model="queryParams.countMonth"
          popper-class="monthStyle"
          type="month"
          value-format="M"
          format="M"
          placeholder="选择月"
        />
      </el-form-item>
      <el-form-item>
        <el-button size="mini" type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button size="mini" icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        <el-button size="mini" type="primary" icon="el-icon-plus" @click="$refs.EditLockMerge.open()" v-if="showBtns.add">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="listLoading" :data="list" size="small" height="calc(100vh - 300px)">
      <el-table-column label="序号" type="index" width="50" />
      <el-table-column label="年份" prop="countYear" align="center" width="80" />
      <el-table-column label="月份" prop="countMonth" align="center" min-width="80" />
      <el-table-column label="开始日期" align="center" width="100" :formatter="row => dateFmt(row.cfgStartTime)" />
      <el-table-column label="结束日期" align="center" width="100" :formatter="row => dateFmt(row.cfgEndTime)" />
      <el-table-column label="设计状态" align="center" width="70">
        <div slot-scope="{ row }">
          <div @click="toggleIsEnable(row, 'designLock')" style="display: inline-block; cursor: pointer" v-if="row.isEditable">
            <i class="el-icon-loading" v-if="!!row.designLockLoading" />
            <el-switch class="g-switch-in" v-model="row.designLock" :active-value="1" :inactive-value="0" active-text="锁定" inactive-text="未锁" :width="50" style="pointer-events: none" v-else />
          </div>
          <el-tag size="small" :type="row.designLock ? 'primary' : 'info'" v-else>{{ row.designLock ? '锁定' : '未锁' }}</el-tag>
        </div>
      </el-table-column>
      <el-table-column label="工程状态" align="center" width="70">
        <div slot-scope="{ row }">
          <div @click="toggleIsEnable(row, 'engineerLock')" style="display: inline-block; cursor: pointer" v-if="row.isEditable">
            <i class="el-icon-loading" v-if="!!row.engineerLockLoading" />
            <el-switch class="g-switch-in" v-model="row.engineerLock" :active-value="1" :inactive-value="0" active-text="锁定" inactive-text="未锁" :width="50" style="pointer-events: none" v-else />
          </div>
          <el-tag size="small" :type="row.engineerLockLoading ? 'primary' : 'info'" v-else>{{ row.engineerLockLoading ? '锁定' : '未锁' }}</el-tag>
        </div>
      </el-table-column>
      <el-table-column label="品质状态" align="center" width="70">
        <div slot-scope="{ row }">
          <div @click="toggleIsEnable(row, 'mergeLock')" style="display: inline-block; cursor: pointer" v-if="row.isEditable">
            <i class="el-icon-loading" v-if="!!row.mergeLockLoading" />
            <el-switch class="g-switch-in" v-model="row.mergeLock" :active-value="1" :inactive-value="0" active-text="锁定" inactive-text="未锁" :width="50" style="pointer-events: none" v-else />
          </div>
          <el-tag size="small" :type="row.mergeLockLoading ? 'primary' : 'info'" v-else>{{ row.mergeLockLoading ? '锁定' : '未锁' }}</el-tag>
        </div>
      </el-table-column>
      <!-- <el-table-column label="奥哲单据状态" align="center" width="110">
        <dict-tag slot-scope="{ row }" :options="dict.type.trd_az_order_status" :value="row.azStatus" />
      </el-table-column> -->
      <el-table-column label="修改人" prop="updator" align="center" show-overflow-tooltip min-width="90" />
      <el-table-column label="修改时间" prop="updateTime" align="center" width="150" />
      <el-table-column label="创建人" prop="creator" align="center" show-overflow-tooltip min-width="90" />
      <el-table-column label="创建时间" prop="createTime" align="center" width="150" />
      <el-table-column label="备注" prop="remark" align="center" show-overflow-tooltip min-width="150" />
      <el-table-column label="操作" fixed="right" align="center" width="120">
        <template slot-scope="{ row }">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="$refs.EditLockMerge.open(row)" v-if="row.isEditable">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" :loading="row.removeLoading" @click="handleDelete(row)" v-if="row.isRemovable">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <EditLockMerge ref="EditLockMerge" v-model="showEditLockMerge" @ok="getList" />
  </div>
</template>
<script>
// 检查得分-品质得分月结锁定列表
import auth from '@/plugins/auth'
import { TRD_AZ_ORDER_STATUS as STATUS } from '@/utils/baseData'
import { lockPage, lockAddUpdate, lockRemove } from '@/api/checkScore/cqEval'
import EditLockMerge from './EditLockMerge'
// 可更改的状态
const modifiableStatus = [STATUS.CANCELED, STATUS.EXCEPTION, STATUS.WAIT]
// 状态锁列表
const statusLockList = [
  { label: '设计状态', value: 'designLock' },
  { label: '工程状态', value: 'engineerLock' },
  { label: '品质状态', value: 'mergeLock' }
]
export const statusLock2label = statusLockList.reduce((o, i) => !void (o[i.value] = i.label) && o, {})

export default {
  name: 'LockMergeList',
  components: { EditLockMerge },
  dicts: ['trd_az_order_status'],
  data() {
    return {
      listLoading: false,
      total: 0,
      list: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        countMonth: '',
        countYear: ''
      },
      showEditLockMerge: false
    }
  },
  computed: {
    showBtns() {
      // 操作按钮权限
      const permCode = 'trdQcQualityScore:monthLock'
      const add = auth.hasPermi(`${permCode}:add`)
      const edit = auth.hasPermi(`${permCode}:edit`)
      const remove = auth.hasPermi(`${permCode}:remove`)
      return { add, edit, remove }
    }
  },
  methods: {
    async getList() {
      const { showBtns } = this
      this.listLoading = true
      const res = await lockPage(this.queryParams).catch(() => false)
      this.listLoading = false
      if (res === false) return

      const data = res.data.records || []
      data.map(i => {
        i.designLockLoading = false
        i.mergeLockLoading = false
        i.engineerLockLoading = false
        i.removeLoading = false

        const isModifiable = modifiableStatus.includes(i.azStatus)
        i.isEditable = showBtns.edit && isModifiable
        i.isRemovable = showBtns.remove && isModifiable
      })
      this.list = data
      this.total = res.data.total || 0
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    async toggleIsEnable(item, lockItem) {
      const lockState = item[lockItem] ? 0 : 1
      const actionName = lockState === 1 ? '锁定' : '解锁'
      const res1 = await this.$confirm(`确认要${actionName}${item.countYear}年${item.countMonth}月的${statusLock2label[lockItem]}吗？`, '警告', { type: 'warning' }).catch(() => false)
      if (res1 !== 'confirm') return

      const opt = { ...item }
      opt[lockItem] = lockState
      const lockItemLoading = lockItem + 'Loading'
      item[lockItemLoading] = true
      const res = await lockAddUpdate(opt).catch(() => false)
      item[lockItemLoading] = false
      if (res === false) return

      this.$message.success(`${actionName}成功`)
      this.getList()
    },
    async handleDelete(item) {
      if (item.removeLoading) return
      const res1 = await this.$confirm(`确认删除：${item.countYear}-${item.countMonth}?`, '警告', { type: 'warning' }).catch(() => false)
      if (res1 !== 'confirm') return

      item.removeLoading = true
      const res = await lockRemove(item.id).catch(() => false)
      item.removeLoading = false
      if (res === false) return

      this.msgSuccess('删除成功')
      this.list.length > 1 ? this.getList() : this.handleQuery()
    }
  }
}
</script>
<style lang="scss">
  .monthStyle{
    .el-date-picker__header--bordered {
      display: none !important;
    }
  }
</style>
